# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import time
from tortoise import fields
from kernels.model import DbModel


class SysConfigModel(DbModel):
    id = fields.IntField(pk=True, unsigned=True, description="主键")
    type = fields.CharField(null=False, max_length=100, default="", description="类型")
    key = fields.CharField(null=False, max_length=100, default="", description="键名")
    value = fields.TextField(default="", description="键名")
    remarks = fields.CharField(null=False, max_length=100, default="", description="备注")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")

    class Meta:
        table_description = "系统配置表"
        table = DbModel.table_prefix("sys_config")


class SysCrontabModel(DbModel):
    id = fields.IntField(pk=True, unsigned=True, description="主键")
    name = fields.CharField(null=False, max_length=64, default="", description="任务名称")
    command = fields.CharField(null=False, max_length=200, default="", description="执行命令")
    params = fields.CharField(null=False, max_length=200, default="", description="附带参数")
    trigger = fields.CharField(null=False, max_length=200, default="", description="触发类型")
    rules = fields.TextField(null=False, default="", description="运行规则")
    remarks = fields.CharField(null=False, max_length=300, default="", description="备注信息")
    error = fields.TextField(default="", description="错误提示")
    concurrent = fields.SmallIntField(null=False, default=1, description="并发数量")
    status = fields.SmallIntField(null=False, default=1, description="执行状态: [1=运行, 2=暂停, 3=错误]")
    exe_time = fields.FloatField(null=False, default=0, description="执行时长")
    max_time = fields.FloatField(null=False, default=0, description="最大执行时长")
    last_time = fields.IntField(null=False, default=0, description="最后执行时间")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "系统任务表"
        table = DbModel.table_prefix("sys_crontab")

    @classmethod
    async def compute(cls, tid: int, start_time: float, status: int = 1, error: str = ""):
        """
        任务完成更新信息

        Args:
            tid (int): 任务ID
            start_time (float): 开始时间
            status (int): 任务状态: [1=运行中, 2=已暂停, 3=出错了]
            error (str): 错误信息

        Author:
            zero
        """
        cron: cls = await cls.filter(id=tid).first()

        if cron:
            exe_status: int = cron.status if cron.status != 1 else status
            exe_time: float = round(time.time() - start_time, 4)
            max_time: int = cron.max_time if cron.max_time > exe_time else exe_time
            await cls.filter(id=tid).update(
                exe_time=exe_time,
                max_time=max_time,
                last_time=int(time.time()),
                status=exe_status,
                error=error
            )


class SysLogModel(DbModel):
    id = fields.IntField(pk=True, unsigned=True, description="主键")
    admin_id = fields.IntField(null=False, default=0, description="操作人员")
    summary = fields.CharField(null=False, max_length=100, default="", description="摘要信息")
    endpoint = fields.CharField(null=False, max_length=300, default="", description="请求函数")
    method = fields.CharField(null=False, max_length=30, default="", description="请求方法: [GET,POST]")
    url = fields.CharField(null=False, max_length=100, default="", description="请求路由")
    ip = fields.CharField(null=False, max_length=100, default="", description="请求IP")
    ua = fields.CharField(null=False, max_length=100, default="", description="请求UA")
    user_agent = fields.CharField(null=False, max_length=900, default="", description="UA详情")
    params = fields.TextField(default="", description="请求参数")
    error = fields.TextField(default="", description="错误信息")
    status = fields.SmallIntField(null=False, default=1, description="执行状态: [1=成功, 2=失败]")
    start_time = fields.CharField(null=False, default="0", max_length=20, description="开始时间: 毫秒")
    end_time = fields.CharField(null=False, default="0", max_length=20, description="结束时间: 毫秒")
    task_time = fields.CharField(null=False, default="0", max_length=20, description="耗时时间: 毫秒")
    create_time = fields.IntField(null=False, default=0, description="操作时间")

    class Meta:
        table_description = "系统日志表"
        table = DbModel.table_prefix("sys_log")


class SysCensorModel(DbModel):
    id = fields.IntField(pk=True, unsigned=True, description="主键")
    user_id = fields.IntField(null=False, default=0, description="来源用户")
    channel = fields.CharField(null=False, max_length=10, default="", description="审查通道: [baidu, aliyun]")
    input_type = fields.CharField(null=False, max_length=10, default="", description="输入类型: [text, image]")
    input_content = fields.TextField(description="输入内容")
    censor_result = fields.TextField(description="审查结果")
    ip = fields.CharField(null=False, max_length=64, default="", description="操作IP")
    create_time = fields.IntField(null=False, default=0, description="操作时间")

    class Meta:
        table_description = "系统审查表"
        table = DbModel.table_prefix("sys_censor")


class SysSensitiveModel(DbModel):
    id = fields.IntField(pk=True, unsigned=True, description="主键")
    admin_id = fields.IntField(null=False, default=0, description="管理ID")
    scene = fields.SmallIntField(null=False, default=1, description="场景: [1=敏感词, 2=账号]")
    word = fields.CharField(null=False, max_length=200, default="", description="词汇")
    is_disable = fields.SmallIntField(null=False, default=0, description="是否禁用: [0=否, 1=是]")
    is_delete = fields.SmallIntField(null=False, default=0, description="是否删除: [0=否, 1=是]")
    create_time = fields.IntField(null=False, default=0, description="创建时间")
    update_time = fields.IntField(null=False, default=0, description="更新时间")
    delete_time = fields.IntField(null=False, default=0, description="删除时间")

    class Meta:
        table_description = "系统敏感词"
        table = DbModel.table_prefix("sys_sensitive")

